package tmproject.hlhj.fhp.cartrans.fragments

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import kotlinx.android.synthetic.main.fgm_exam.view.*
import tmproject.hlhj.fhp.cartrans.BaseFgm
import tmproject.hlhj.fhp.cartrans.IView.ExamView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.activitys.ExamRuleAty
import tmproject.hlhj.fhp.cartrans.adapter.ExamFgmListAdp
import tmproject.hlhj.fhp.cartrans.adapter.LearnListAdp
import tmproject.hlhj.fhp.cartrans.javaBean.ExamListBean
import tmproject.hlhj.fhp.cartrans.presenter.ExamPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class ExamFgm:BaseFgm<ExamView,ExamPresenter>(), ExamView, SpringView.OnFreshListener, LearnListAdp.JumpListener {
    private val datas=ArrayList<ExamListBean.DataBean.ListBean>()
    private lateinit var adp:ExamFgmListAdp
    private var page=1
    private var pageSize=10
    private var hasMore=true
    override fun bindView(): ExamView {
        return this
    }
    override fun createPresenter(): ExamPresenter {
        return ExamPresenter()
    }
    override fun getContentId(): Int {
        return R.layout.fgm_exam
    }
    override fun initView() {
        adp= ExamFgmListAdp(datas)

        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=adp

        rootView.spView.header=DefaultHeader(context)
//        rootView.spView.footer=DefaultFooter(context)
        adp.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                if (hasMore){
                    page++
                    presenter?.getExams(context!!,SpHelper.getToken(context!!),page,pageSize)
                }else{
                    adp.loadMoreEnd()
                }
            }
        })

    }
    override fun setExamList(examListBean: ExamListBean) {
        rootView.spView.onFinishFreshAndLoad()
        adp.loadMoreComplete()
        datas.addAll(examListBean.data.list)
        adp.notifyDataSetChanged()
    }

    override fun logicStart() {
    }

    override fun setListener() {
        /*adp.setOnItemClickListener { adapter, view, position ->

        }*/
        adp.listener=this
        rootView.spView.setListener(this)
    }

    override fun jump(intent: Intent) {
        startActivityForResult(intent,1)
    }

    override fun onLoadmore() {
        page++
        presenter?.getExams(context!!,SpHelper.getToken(context!!),page,pageSize)

    }

    override fun onResume() {
        page=1
        datas.clear()
        presenter?.getExams(context!!,SpHelper.getToken(context!!),page,pageSize)

        super.onResume()

    }

    override fun noMore() {
        hasMore=false
        adp.loadMoreEnd()
        rootView.spView.onFinishFreshAndLoad()
    }

    override fun onRefresh() {
        page=1
        hasMore=true
        adp.setEnableLoadMore(true);
        datas.clear()
        presenter?.getExams(context!!,SpHelper.getToken(context!!),page,pageSize)

    }
}